using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Module.CMS.Library;
using Framework.Library;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Web.HomePage
{
    public partial class Navbar : CMSWebControl
    {
        public string hplMenuParrent = "";
        public string hplGioithieu = "";
        public DataTable dt = null;
        public DataTable dt1 = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (curZone.Zone_ParentID != 0)
            {
                ZoneInfo _dzon = ZoneController.GetZone(curZone.Zone_ParentID);
                if (_dzon.Zone_ParentID != 0)
                {
                    this.dt = ZoneController.GetZones(_dzon.Zone_ParentID, AppEnv.PortalId());
                }
                else
                {
                    this.dt = ZoneController.GetZones(_dzon.Zone_ID, AppEnv.PortalId());
                }
            }
            else
            {
                this.dt = ZoneController.GetZones(curZone.Zone_ID, AppEnv.PortalId());
            }
            //this.dt = ZoneController.GetZonesVisbleInNavbar(ConvertUtility.ToInt32(AppEnv.PortalId()));
            if (GetZoneParrentID(curZone.Zone_ID) == Convert.ToInt32(Request.QueryString["catid"]))
            {
                if (curZone.Zone_ParentID != 0)
                {
                    hplMenuParrent = "<td background=\"/images/HomePage/bg_tab.jpg\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"40\" height=\"28\">&nbsp;</td><td class=\"text5\">" + GetZoneName(curZone.Zone_ID) + "</td></tr></table></td>";
                }
                else
                {
                    hplMenuParrent = "<td background=\"/images/HomePage/bg_tab.jpg\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"40\" height=\"28\">&nbsp;</td><td class=\"text5\">" + curZone.Zone_Name + "</td></tr></table></td>";
                }
            }
            else
            {
                if (curZone.Zone_ParentID != 0)
                {
                    hplMenuParrent = "<td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"40\" height=\"28\">&nbsp;</td><td><a id='hplMenuParrent' class='text4' href=\"" + BuildUrlByZone(GetZoneParrentID(curZone.Zone_ID), GetZoneParrentTabID(curZone.Zone_ID)) + "\">" + GetZoneName(curZone.Zone_ID) + "</a></td></tr></table></td>";
                }
                else
                {
                    hplMenuParrent = "<td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td width=\"40\" height=\"28\">&nbsp;</td><td><a id='hplMenuParrent' class='text4' href=\"" + BuildUrlByZone(curZone.Zone_ID, curZone.Portal_TabControlID) + "\">" + curZone.Zone_Name + "</a></td></tr></table></td>";
                }
            }
            DataListintro.DataSource = dt;
            DataListintro.DataBind();
        }
        protected void OnItemDatabounds(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            HyperLink hplActive = (HyperLink)e.Item.FindControl("hplActive");

            hplActive.NavigateUrl = BuildUrlByZone((ConvertUtility.ToInt32(row["Zone_ID"])), ConvertUtility.ToInt32(row["Portal_TabControlID"]));
            hplActive.Text = Convert.ToString(row["Zone_Name"]);
            Table tblActive = (Table)e.Item.FindControl("tblActive");
            if (Convert.ToInt32(row["Zone_ID"]) != Convert.ToInt32(Request.QueryString["catid"]))
            {
                tblActive.BackImageUrl = "";
                hplActive.CssClass = "text4";
            }
            else
            {
                hplActive.CssClass = "text5";
                tblActive.BackImageUrl = "/images/HomePage/bg_tab.jpg";
            }

            Label m_lableID = (Label)e.Item.FindControl("lblID");
            Int32 m_ID = Convert.ToInt32(m_lableID.Text.ToString());
            this.dt1 = ZoneController.GetZones(ConvertUtility.ToInt32(m_ID), AppEnv.PortalId());

            DataList SqlDataS = (DataList)e.Item.FindControl("DataListItem");
            SqlDataS.DataSource = dt1;
            SqlDataS.ItemDataBound += new DataListItemEventHandler(SqlDataS_ItemDataBound);
            SqlDataS.DataBind();

        }
        private void SqlDataS_ItemDataBound(object o, DataListItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            HyperLink hplActive = (HyperLink)e.Item.FindControl("hplActive1");
            Table tblActive = (Table)e.Item.FindControl("tblActive1");
            hplActive.NavigateUrl = BuildUrlByZone((ConvertUtility.ToInt32(row["Zone_ID"])), ConvertUtility.ToInt32(row["Portal_TabControlID"]));
            hplActive.Text = Convert.ToString(row["Zone_Name"]);
            if (Convert.ToInt32(row["Zone_ID"]) != Convert.ToInt32(Request.QueryString["catid"]))
            {
                hplActive.CssClass = "text4";
            }
            else
            {
                hplActive.CssClass = "text5";
                tblActive.BackImageUrl = "/images/HomePage/bg_tab.jpg";
            }
        }
        private int GetZoneParrentID(int ID)
        {
            ZoneInfo info = ZoneController.ZoneInfo(ID);
            if (info.Zone_ParentID != 0)
            {
                int _return = 0;
                _return = GetZoneParrentID(info.Zone_ParentID);
                return _return;
            }
            return info.Zone_ID;
        }
        private int GetZoneParrentTabID(int TabID)
        {
            ZoneInfo info = ZoneController.ZoneInfo(TabID);
            if (info.Zone_ParentID != 0)
            {
                int _return = 0;
                _return = GetZoneParrentTabID(info.Zone_ParentID);
                return _return;
            }
            return info.Portal_TabControlID;
        }
        private string GetZoneName(int ID)
        {
            ZoneInfo info = ZoneController.ZoneInfo(ID);
            if (info.Zone_ParentID != 0)
            {
                string _return = "";
                _return = GetZoneName(info.Zone_ParentID);
                return _return;
            }
            return info.Zone_Name;
        }      
    }
}